<template>
  <div>
    <el-table :data="tableData"
              style="width: 100%"
              border>
      <el-table-column label=""
                       type="expand"
                       width="48">
        <template slot-scope="scope">
          <Tag :dynamicTags='scope.row.attr_vals'
               :id='scope.row.attr_id'
               :attrId='scope.row.cat_id'
               :attr_sel='scope.row.attr_sel'
               :attr_name='scope.row.attr_name'></Tag>
        </template>
      </el-table-column>
      <el-table-column label="#"
                       type="index"
                       width="48">
      </el-table-column>
      <el-table-column label="参数名称"
                       width="494">
        <template slot-scope="scope">
          <p>{{ scope.row.attr_name }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="493">
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="'添加'+labelName"
               :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item :label="labelName"
                      :label-width="formLabelWidth">
          <el-input v-model="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary"
                   @click="changeparams">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Tag from '@/components/Tag.vue'
import { editparams, delparams } from '@/api/Param.js'
export default {
  data () {
    return {
      formLabelWidth: '120px',
      dialogFormVisible: false,
      form: {
        name: ''
      },
      tableItem: {},
      index: 0
    }
  },
  components: {
    Tag
  },
  props: {
    tableData: {
      type: Array,
      require: true
    },
    labelName: {
      type: String,
      require: true
    }
  },
  created () {

  },
  methods: {
    handleEdit (index, row) {
      this.tableItem = row
      this.form.name = row.attr_name
      this.dialogFormVisible = true
      console.log(index, row)
      this.index = index
    },
    async handleDelete (index, row) {
      console.log(index, row)
      await delparams(row.cat_id, row.attr_id)
      this.tableData.splice(index, 1)
    },
    async changeparams () {
      if (this.form.name === '') return
      await editparams(this.tableItem.cat_id, this.tableItem.attr_id, {
        attr_name: this.form.name,
        attr_sel: this.tableItem.attr_sel
      })
      this.tableData[this.index].attr_name = this.form.name
      this.dialogFormVisible = false
    }

  }
}
</script>

<style scoped lang='less'>
</style>
